package UI_admin;

import GestionBilleterie.Client;
import java.io.IOException;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import outils.ExcelExporter;

/**
 * Panel permettant de visualiser les clients
 * @author Elsa Ferrare, Alexandre Becquet
 */
public class Panel_client extends javax.swing.JPanel {

    /**
     * Constructeur d'un panel pour visualiser les clients
     */
    public Panel_client() {
        initComponents();
        TableModelClients tableCli=new TableModelClients(Client.findEntities());
        this.jTable1.setModel(tableCli);
        this.jScrollPane1.revalidate();
    }

    /**
     * Classe interne pour la création du tablau des clients
     */
    class TableModelClients extends AbstractTableModel {
        List<Client> mesClients;
        private String[] columnNames = {"Id",
                                        "Nom Prenom",
                                        "Date Naissance",
                                        "Adresse",
                                        "Tel",
                                        "Email"};

        private TableModelClients(List<Client> lesClients) {
            this.mesClients=lesClients;
        }

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return this.mesClients.size();
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
           Object value = null;
           if(col==0)
               value=this.mesClients.get(row).getId();
           else if(col==1)
               value=this.mesClients.get(row).getNom() + " " + this.mesClients.get(row).getPrenom() ;
           else if(col==2)
               value=this.mesClients.get(row).getDateNaissance();
           else if(col==3)
               value=this.mesClients.get(row).getAdresse();
           else if(col==4)
               value=this.mesClients.get(row).getNumTelephone();
           else if(col==5)
               value=this.mesClients.get(row).getEmail();
           return value;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButtonExportExcel = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jButtonExportExcel.setText("Export Excel");
        jButtonExportExcel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonExportExcelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(307, Short.MAX_VALUE)
                .addComponent(jButtonExportExcel))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jButtonExportExcel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Action du Button ExportExcel
     * Export du tableau en Excel et ouverture de ce fichier
     * @param evt
     */
    private void jButtonExportExcelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExportExcelActionPerformed
        try {
            ExcelExporter exp = new ExcelExporter("clients.xls");
            exp.exportTable(this.jTable1);
            exp.openTable();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
}//GEN-LAST:event_jButtonExportExcelActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonExportExcel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

}
